import {expect} from "chai"
import {v1} from "uuid"
import {fromJS,Map,List} from "immutable"

import coreReducer from '../../src/server/reducer.js'
import {addRoom,removeRoom} from "../../src/server/actionCreator.js"

/* ES5 Reduce
var array1 = [1,2,3,4,5]
var array2 = [1,2,3,4,5]

array1.reduce(function(prev, current) {
    return prev + current
}, 0)

array2.reduce(function(obj, current) {
    obj[current] = current;
}, {})

console.log(array1);
*/

describe("server端核心Reducer", ()=>{
    
    it("可以当做一个reducer", ()=>{
        
        var id = v1();
        var actions = [
            {type: 'ADD_ROOM', room: {id, name:"1", owner:"owner1"}},
            {type: 'ADD_ROOM', room: {name:"2", owner:"owner2"}},
            {type: 'ADD_ROOM', room: {name:"3", owner:"owner3"}},
            {type: 'REMOVE_ROOM', payload: {id:id, user:"owner1"}},
        ]

        const finalState = actions.reduce( coreReducer, undefined )

        expect( finalState.get("rooms").size ).to.equal(2)
        expect( finalState.getIn(["rooms", 0, "owner"]) ).to.equal("owner2")
    })

    it("使用actionCreator", ()=>{
        
        var id = v1();
        var actions = [
            addRoom({id, name:"1", owner:"owner1"}),
            addRoom({name:"2", owner:"owner2"}),
            addRoom({name:"3", owner:"owner3"}),
            removeRoom({id:id, user:'owner3'})
        ]

        const finalState = actions.reduce( coreReducer, undefined )

        expect( finalState.get("rooms").size ).to.equal(3)
    })

})

